
	/*
	 * This can save you the tedium of typing name='xxx' id='xxx', by automatically
	 * setting the id of your form element to whatever the element's name is. 
	 * This won't set an id if the id already exists on some element, and if several
	 * elements have the same name, it will only set the id on the first element.
	 */
	(function($) {
		$.addIds = function(aForm) {
			var form = aForm || document.forms[0]
		    $('form *').each(function(){
		    	//If the element's name hasn't already been used as an id
		    	//(either by the element or by some other element) ...
		    	//and the element has a name 
		    	if( $('#'+this.name).length === 0 && this.name !== undefined && this.name !== "" ){
		    		//If there's just one element with this name, set
		    		//that element's id to this name
		    		//If there are several such elements, as with a checkbox (I think)
		    		//then set the id of the first one to the current name
		    		//All this assuming that the first named element doesn't already
		    		//have an id.
			        var named = document.getElementsByName(this.name)
					if(named.length > 0 &&  named[0].id === ''){
				        	named[0].id = this.name
					}
		    	}
		    });
	    }
	})(jQuery);
	  
	  
	//Dojo dependency!!
	//Note that we either need to use the same dialog on repeated calls
	//or kill the old one and create a new one.  Otherwise we could 
	//show a popup that has an form inside it, the user could submit the
	//form with data that makes it fail validation, and then a second dialog
	//would be created.  That would make two dialogs, and we don't want that.
	//Calls to the same dialog on subsequent calls, rather than
	//killing the old one and creating a new one, which would make
	//it look like the dialog dissapears and reappears again, which may
	//be off-putting to the user. 
	//We add the dialog to the window object.  The dialog's property name
	//is 'theDialog' by default, and you can change it by passing in a
	//'theDialog' property on the options argument.  You'll need to do this
	//if you want to have more than one dialog open at the same time.
	function popup(text, options){
		var settings = $.extend(
				{theDialog : 'theDialog'}, 
				options || {}
		)
		
		//Taken from spring-dojo.js
		var tempSpan = dojo.doc.createElement("span");
		tempSpan.id="ajaxResponse";
		tempSpan.style.visibility= "hidden";
		document.body.appendChild(tempSpan);
		tempSpan.innerHTML=text;
		var tempContainer = new dojo.NodeList(tempSpan);
		var newNodes = tempContainer.query("#ajaxResponse > *").orphan();
		tempContainer.orphan();

		if(window[settings.theDialog] === undefined || window[settings.theDialog] === ""){
			window[settings.theDialog] = new dijit.Dialog({});
		}
		window[settings.theDialog].setContent(newNodes);
		window[settings.theDialog].show();
		
		//If we ever wanted to kill the old dialog and bring up a new one,
		//we'd kill like so:
		//dialog.hide()
		//dialog.destroyRecursive(false)
		
		
	}
	
	
	(function($) {
		$.addPartials = function(data, options) {
			var settings = $.extend(
					{replaceMeClass : 'replaceMe'}, 
					options || {}
			)
			//TODO use options where replaceMe is the default class.
			$('.'+settings.replaceMeClass).each(function(){
				var wasOriginallyUndefined = false
				 //First let's see if we there's a data.'element.id'
				var content = data[this.id]
			    if(content === undefined){
			    	wasOriginallyUndefined = true
					content = ""
			    }
			    //If elemen.id starts with 'keep_', we won't erase it's previous value
			    // even if it wasn't sent up from the server this time.  In all other instances
		        //we apply the content.
			    if(! (keep(this.id) && wasOriginallyUndefined) ){
			    	//Note that the jquery html function will actually evaluate
			    	//any script element that happens to be present!  Doing
			    	//this.innerHTML = content would not have that effect.
			    	//However, evalScriptFromJson is the prefered way to
			    	//evaluate javascript here.
			    	$('#'+this.id).html(content)
			    }	
			});
	    }
	})(jQuery);
	
	(function($) {
		$.evalScriptFromJson = function(data) {
		    if( data.javascript !== undefined){
		    	eval(data.javascript)
		    }
		}
	})(jQuery);
	
	(function($) {
		$.remoteForm = function(formId, callback) {
			var cb = callback || $.respondToJson 
			$('#'+formId).each(function(){
				var submitButtonSelect = '#' + this.id + ' input[type=submit]'
				$(submitButtonSelect).click(function(event){
					var serializedForm =  $('#'+formId).formSerialize()
					var formAction = $('#' + formId)[0].action
					var formMethod = $('#' + formId)[0].method
					//Chuck TODO These are too limited.  Need to use $.ajax soon.
					var jqueryMethod = (formMethod === 'get') ? $.get : $.post
					jqueryMethod(formAction, serializedForm, cb, "json") 		
					return false
				})
			})
		}
	})(jQuery);	
	
	/**
	 * Use this if you have several pages on a form and want to focus the browser
	 * on the form that has changed.
	 */
	(function($) {
		$.focusOnForm = function(formId) {
			$('#'+formId)
			  .append('<input type="button" name="button1" id="xxxpd" value="Button1" />')
			$('#xxxpd')[0].focus()
			$('#xxxpd').remove()			
		}
	})(jQuery);	
	
	(function($) {
		$.respondToJson = 	function(data){
			var dialogName = 'theDialog'
			if(data.redirectURL !== undefined){
				window.location = window.location.protocol + "//" + window.location.host + data.redirectURL;
				//This bit only seems to be needed if the url directs us to a spot on the very same
				//page we were previously.  CHUCK TODO: We never use the
				//trick of redirecting to a specific spot on a page, so let's get rid of
				//this.
				if(window[dialogName] !== undefined && window[dialogName] !== ""){
					window[dialogName].hide()
					window[dialogName].destroyRecursive(false)					
				}
				return;
			}	
			else if(data.popup !== undefined){
				popup(data.popup, {theDialog : dialogName})
			}
			else
				$.addPartials(data)
			$.evalScriptFromJson(data)
	    }	
	})(jQuery);		

	
	function keep(str){
		var arr = str.split('_')
		if(arr.length == 0)
			return false
		return arr[0] == 'keep'
	
	}
